//
//  SearchListViewController.m
//  CarLife
//
//  Created by yu on 2017/11/16.
//  Copyright © 2017年 jpas. All rights reserved.
//

#import "SearchListViewController.h"

#import "NearSelectView.h"

#import "SelectListViewController.h"

#import "NaviViewController.h"
#import "SpeechSynthesizer.h"

#import "RoutePlanViewController.h"

#import "DriveManager.h"

@interface SearchListViewController ()<SelectedTableViewCellDelegate,AMapNaviDriveManagerDelegate,DriveNaviDelegate>

@property (nonatomic, strong) NearSelectView *parkingView;

@property (nonatomic, strong) ParkingListView *parkingListView;

@property (nonatomic, strong) AMapNaviDriveManager *driveManager;

@property (nonatomic, strong) MAMapView *mapview;

@end

@implementation SearchListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = self.naviItemTitle;
    
    [self setupParkingListView];
    
    [self updateSubviewsFrame];
    
//    [self initDriveManager];
    
    [self.mapView showAnnotations:self.annotations edgePadding:UIEdgeInsetsMake(70, 70, 370, 70) animated:NO];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(directNaviButtonAction:) name:@"kDirectNaviButtonAction" object:nil];
}


#pragma mark -Cell导航按钮的通知
- (void)directNaviButtonAction:(NSNotification *)noti
{
    self.selectPoint = [AMapNaviPoint locationWithLatitude:[noti.userInfo[@"latitude"] floatValue] longitude:[noti.userInfo[@"longitude"] floatValue]];
    [self navigationAction];
//    CLNavigationViewController *navi = [[CLNavigationViewController alloc] init];
//    navi.destinationPoint = [[CLLocation alloc] initWithLatitude:self.selectPoint.latitude longitude:self.selectPoint.longitude];
//    [self.navigationController pushViewController:navi animated:YES];
}

- (void)navigationAction
{
    PPLog(@"666 == %lf/%lf",self.selectPoint.latitude,self.selectPoint.longitude);
    [[AMapNaviDriveManager sharedInstance] setDelegate:self];
    [[AMapNaviDriveManager sharedInstance] calculateDriveRouteWithEndPoints:@[self.selectPoint]
                                              wayPoints:nil drivingStrategy:AMapNaviDrivingStrategySingleDefault];
}

//- (void)initDriveManager
//{
////    self.driveManager = [DriveManager sharedManager];
//    self.driveManager = [AMapNaviDriveManager sharedInstance];
//    [self.driveManager setDelegate:self];
//}


#pragma mark -SelectedTableViewCellDelegate

- (void)selectedTableViewCellAction:(SearchAnnotation *)annotation
{
    SelectListViewController *select = [[SelectListViewController alloc] init];
    select.selectAnnotation = annotation;
    select.annotations = self.annotations;
    [self.navigationController pushViewController:select animated:NO];
}

- (void)setupParkingListView
{
    if (self.parkingListView == nil) {
        self.parkingListView = [[ParkingListView alloc] initWithFrame:CGRectMake(0, kScreenHeight - 64 -300, kScreenWidth, 300)];
        [self.parkingListView setPois:self.annotations];
        self.parkingListView.delegate = self;
        [self.view addSubview:self.parkingListView];
        //下滑tableView收起，显示"点击展开更多"
        @weakify(self);
        self.parkingListView.moveBlock = ^{
            @strongify(self);
            self.otherBtnContainer.hidden = NO;
            [self.parkingListView showParkingListView:YES differ:45];
            [self.zoomBtnContainer sd_clearAutoLayoutSettings];
            self.zoomBtnContainer.sd_layout
            .bottomSpaceToView(self.parkingListView, 10);
            [self.zoomBtnContainer updateLayout];
    
            [self.mapView showAnnotations:self.annotations edgePadding:UIEdgeInsetsMake(40, 40, 90, 40) animated:NO];
        };
        
        // 展开更多
        self.parkingListView.showMoreBlock = ^{
            @strongify(self);
            [self.parkingListView showParkingListView:YES differ:300];
            [self.mapView showAnnotations:self.annotations edgePadding:UIEdgeInsetsMake(70, 70, 360, 70) animated:NO];
            
            self.userLocBtn.sd_layout
            .bottomSpaceToView(self.parkingListView, 10);
            [self.userLocBtn updateLayout];
        };
    }
}

- (void)updateSubviewsFrame
{
    self.userLocBtn.sd_layout.bottomSpaceToView(self.parkingListView, 10);
    self.zoomBtnContainer.sd_layout.bottomSpaceToView(self.parkingListView, 10);
}

- (void)mapView:(MAMapView *)mapView didSelectAnnotationView:(MAAnnotationView *)view {
    SearchAnnotation *annotation = (SearchAnnotation *)view.annotation;
    SelectListViewController *select = [[SelectListViewController alloc] init];
    select.selectAnnotation = annotation;
    select.annotations = self.annotations;
    PPLog(@"88888 = %ld",self.annotations.count);
    [self.navigationController pushViewController:select animated:NO];
}

- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation
{
    if ([annotation isKindOfClass:[SearchAnnotation class]] )
    {
        static NSString *pointReuseIndetifier = @"ParkingAnnotationView";
        MAAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndetifier];
        
        if (annotationView == nil)
        {
            annotationView = [[MAAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pointReuseIndetifier];
        }
        annotationView.canShowCallout = NO;
        annotationView.draggable = NO;
        annotationView.image = IMAGE_NAMED(@"near_position_nor");
        
        return annotationView;
    }
    return nil;
}

#pragma mark - DriveNaviView Delegate

- (void)driveNaviViewCloseButtonClicked
{
    // 开始导航后不再允许选择路径，所以停止导航
    [[AMapNaviDriveManager sharedInstance] stopNavi];
    [[SpeechSynthesizer sharedSpeechSynthesizer] stopSpeak];
    [self.navigationController popViewControllerAnimated:NO];
}

#pragma mark - AMapNaviDriveManager Delegate

- (void)driveManager:(AMapNaviDriveManager *)driveManager error:(NSError *)error
{
    PPLog(@"error:{%ld - %@}", (long)error.code, error.localizedDescription);
}

//TODO:规划路线成功的回调
- (void)driveManagerOnCalculateRouteSuccess:(AMapNaviDriveManager *)driveManager
{
//    NaviViewController *driveVC = [[NaviViewController alloc] init];
//    [driveVC setDelegate:self];
//
//    //将driveView添加为导航数据的Representative，使其可以接收到导航诱导数据
//    [self.driveManager addDataRepresentative:driveVC.driveView];
//
//    [self.navigationController pushViewController:driveVC animated:YES];
////    [self.driveManager setEmulatorNaviSpeed:120];
//    [self.driveManager startGPSNavi];
    
    NaviViewController *driveVC = [[NaviViewController alloc] init];
           [driveVC setDelegate:self];
           
           //将driveView添加为导航数据的Representative，使其可以接收到导航诱导数据
           [[AMapNaviDriveManager sharedInstance] addDataRepresentative:driveVC.driveView];
           
           [self.navigationController pushViewController:driveVC animated:NO];
           //        [self.driveManager setEmulatorNaviSpeed:120];
           [[AMapNaviDriveManager sharedInstance] startGPSNavi];
    
}

- (void)driveManager:(AMapNaviDriveManager *)driveManager onCalculateRouteFailure:(NSError *)error
{
    [MBProgressHUD showToast:error.description];
    PPLog(@"onCalculateRouteFailure:{%ld - %@}", (long)error.code, error.localizedDescription);
}

- (void)driveManager:(AMapNaviDriveManager *)driveManager didStartNavi:(AMapNaviMode)naviMode
{
    PPLog(@"didStartNavi");
}

- (void)driveManagerNeedRecalculateRouteForYaw:(AMapNaviDriveManager *)driveManager
{
    PPLog(@"needRecalculateRouteForYaw");
}

- (void)driveManagerNeedRecalculateRouteForTrafficJam:(AMapNaviDriveManager *)driveManager
{
    PPLog(@"needRecalculateRouteForTrafficJam");
}

- (void)driveManager:(AMapNaviDriveManager *)driveManager onArrivedWayPoint:(int)wayPointIndex
{
    PPLog(@"onArrivedWayPoint:%d", wayPointIndex);
}

- (BOOL)driveManagerIsNaviSoundPlaying:(AMapNaviDriveManager *)driveManager
{
    return [[SpeechSynthesizer sharedSpeechSynthesizer] isSpeaking];
}

- (void)driveManager:(AMapNaviDriveManager *)driveManager playNaviSoundString:(NSString *)soundString soundStringType:(AMapNaviSoundType)soundStringType
{
    PPLog(@"playNaviSoundString:{%ld:%@}", (long)soundStringType, soundString);
    [[SpeechSynthesizer sharedSpeechSynthesizer] speakString:soundString];
}

- (void)driveManagerDidEndEmulatorNavi:(AMapNaviDriveManager *)driveManager
{
    PPLog(@"didEndEmulatorNavi");
}

- (void)driveManagerOnArrivedDestination:(AMapNaviDriveManager *)driveManager
{
    PPLog(@"onArrivedDestination");
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
